package com.uenpay.oem.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.socks.library.KLog;
import com.uenpay.oem.R;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.service.bean.TransBankCardList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by PS on 2017/6/18.
 */

public class RecharageAdapter extends RecyclerView.Adapter<RecharageAdapter.ViewHolder> {
	private static TransBankCardList mBankCards;
	private OnBankItemClickListener listener;
	private int choosePosition = -1;

	public RecharageAdapter(TransBankCardList list) {
		this.mBankCards = list;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = View.inflate(parent.getContext(), R.layout.item_recharge_dialog, null);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(RecharageAdapter.ViewHolder holder, int position) {
		holder.setPosition(position);

		BankCardInfo cardInfo = (BankCardInfo) mBankCards.getBankCardList().get(position);

		if (mBankCards.getIsOpenCredit().equals("1") && cardInfo.getCardType().equals("2")) {
			holder.itemBingo.setVisibility(View.INVISIBLE);
			holder.textView.setTextColor(Color.GRAY);
			holder.rootView.setClickable(false);
		} else {
			if (position == choosePosition) {
				holder.itemBingo.setVisibility(View.VISIBLE);
			} else {
				holder.itemBingo.setVisibility(View.INVISIBLE);
			}
			holder.textView.setTextColor(Color.BLACK);
			holder.rootView.setClickable(true);
		}

		String cardNum = cardInfo.getCardNo();
		String cardEndNum = cardNum.substring(cardNum.length() - 4);
		holder.textView.setText(cardInfo.getBankName() + " (" + cardEndNum + ")");
		holder.itmeIv.setVisibility(View.VISIBLE);
		switch (cardInfo.getBankName()) {
		case "招商银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_zhaoshang);
			break;
		case "中国银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_china);
			break;
		case "中信银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_zhongxin);
			break;
		case "浦发银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_pufa);
			break;
		case "建设银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_jianshe);
			break;
		case "工商银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_gongshang);
			break;
		case "交通银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_jiaotong);
			break;
		case "民生银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_minsheng);
			break;
		case "农业银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_nongye);
			break;
		case "邮政储蓄银行":
			holder.itmeIv.setImageResource(R.drawable.icon_bank_youzheng);
			break;
		default:
			holder.itmeIv.setVisibility(View.INVISIBLE);

		}
	}

	@Override
	public int getItemCount() {
		if (mBankCards != null) {
			return mBankCards.getBankCardList().size();
		}
		return 0;
	}

	class ViewHolder extends RecyclerView.ViewHolder {
		private int position;
		@BindView(R.id.itme_iv)
		ImageView itmeIv;
		@BindView(R.id.item_bingo)
		ImageView itemBingo;
		@BindView(R.id.item_text)
		TextView textView;
		View rootView;

		ViewHolder(View view) {
			super(view);
			ButterKnife.bind(this, view);
			rootView = view;

			view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (listener != null) {
						listener.chooseBank(position);
						choosePosition = position;
					}
				}
			});
		}

		public void setPosition(int position) {
			this.position = position;
		}

	}

	public interface OnBankItemClickListener {
		void chooseBank(int position);
	}

	public void setOnBankItemClickListener(OnBankItemClickListener listener) {
		this.listener = listener;
	}

	public void setChoosePosition(int position) {
		this.choosePosition = position;
		notifyDataSetChanged();
	}
}
